//
//  VisionViewController.swift
//  App_Vision
//
//  Created by Flutter on 2021/7/22.
//

import UIKit
import UICore
import SnapKit
import Leaf
import RxCocoa
import RxSwift
import App_Common
import Center

class VisionViewController: ViewController  {
    
    lazy var enterEyeTestButton: UIButton = {
        UIButton(type: .system)
            .leaf
            .titleText(title: "开始测试", for: .normal)
            .instance
    }()
    

    lazy var personSelectView: PersonSelectView = {
        let selectView = PersonSelectView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 200))
        selectView.delegate = self
        return selectView
    }()
    
    var memberList:[Person] = Person.getPeople() ?? []
    
    
}


// MARK: - Override
extension VisionViewController {
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupNavigationBar(title: String.localized("Vision.ViewController.Title"))
        setupUI()
        
        
//        a.asObserver().bind(to: b).disposed(by: disposeBag)
        
    }
}

// MARK: - UI
private extension VisionViewController {

    func setupUI() {

        view.backgroundColor = #colorLiteral(red: 0.9803921569, green: 0.9803921569, blue: 0.9803921569, alpha: 1)
        
        enterEyeTestButton.leaf.add(to: view).snp.makeConstraints {
            $0.center.equalToSuperview()
            $0.height.equalTo(50)
            $0.width.equalTo(200)
        }
        
        enterEyeTestButton.rx.controlEvent(.touchUpInside).subscribe { [weak self] event in
            let eyeTestVC = EyeTestViewController()
            self?.navigationController?.pushViewController(eyeTestVC, animated: true)
        }.disposed(by: disposeBag)
        
        personSelectView.leaf.add(to: view).snp.makeConstraints {
            $0.leading.trailing.equalToSuperview()
            $0.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            $0.height.equalTo(200)
        }
        personSelectView.dataSource = memberList

    
        
    }

}


extension VisionViewController: PersonSelectViewDelegate {
    
    func personSelectView(_ personSelectView: PersonSelectView, didSelectItemAt index: Int) {
        
        if index >= memberList.count  {

            let vc = AddMemberController()
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else {
            print(memberList[index])
        }
    }
    
}

